/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jobportal.Dbconn;
import jobportal.EmailSender;

/**
 *
 * @author project
 */
public class AddAdimn extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        Connection con;
        HttpSession session;
        Statement st;
        ResultSet rs;
        RequestDispatcher rd = request.getRequestDispatcher("admin_mang.jsp");
        try {
            session = request.getSession();
            con = Dbconn.getConn();
            st = con.createStatement();

            String sql;
            //username=&fname=&email=&password=&password1=&btnsubmit=Add+Member
            String username = request.getParameter("username");

            String email = request.getParameter("email");
            String name = request.getParameter("fname");
            String prevn = request.getParameter("perv");
            rs = st.executeQuery("select prev from admin where userid='" + session.getAttribute("username") + "'");
            String prev = "";
            if (rs.next() && rs.getString("prev").equalsIgnoreCase("full")) {


                Random rdm = new Random();
                long password = rdm.nextLong();

                sql = "insert into admin values('" + username + "','" + password + "','" + name + "','" + email + "',now(),'Active','" + prevn + "')";
                int i = st.executeUpdate(sql);
                if (i == 0) {
                    request.setAttribute("msg", "Registration Failed");
                    rd.forward(request, response);
                } else {
                    request.setAttribute("msg", "Registration Successful");
                    EmailSender.sendMail(email, "New Registration", "Hello "+name+" your username:"+username+" password:"+password);
                    rd.forward(request, response);
                }
            } else {
                request.setAttribute("msg", "You donot have sufficient priveledge to do this action");
                rd.forward(request, response);
            }
        } catch (Exception e) {
           // rd=request.getRequestDispatcher("addadminmember.jsp");
            if (e.toString().contains("cannot be null")) {
                request.setAttribute("msg", "Please fill all the field");
            } else if (e.toString().contains("Duplicate")) {
                request.setAttribute("msg", "User already Registered by this name");
            } else {
                request.setAttribute("msg", e);
            }
            rd.forward(request, response);
        } finally {
            out.close();

        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
